// pages/live/create/create.js
import Http from "../../../utils/api";

const http = new Http;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    live: {
      title: '',
      live_img: '',
      is_record: '',
      start_date: '',
      start_time: '',
      end_date: '',
      end_time: ''
    },
    show: false,
    customItem: [],
    detailed: '请选择',
    tipType: 'toast',
    isSubmitValidate: false,
    alignType: 'start',
    title: [
      { required: true, message: '直播主题', trigger: 'blur' },
      { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' },
    ],
    live_img: { min: 10, max: 255, message: '请上传图片', trigger: 'change' },
    is_record: [
      { type:'number', required: true, message: '录制必选', trigger: 'change' },
    ],
   
  },


  //省市联动
  bindRegionChange: function (e) {
    var that = this
    //为了让选择框有个默认值，    
    that.setData({
      clas: ''
    })　　　//下拉框所选择的值
    console.log('picker发送选择改变，携带值为', e.detail.value)

    this.setData({
      //拼的字符串传后台
      detailed: e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2],
      //下拉框选中的值
      region: e.detail.value
    })

    this.setData({
      "AddSite.area": e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2]
    })
    console.log(this.data.AddSite)
  },

  bindStartDateChange: function(e) {
    let live = this.data.live;
    live.start_date = e.detail.value
    this.setData({live});
  },
  bindEndDateChange: function(e) {
    let live = this.data.live;
    live.end_date = e.detail.value
    this.setData({live});
  },
  bindStartTimeChange: function(e) {
    let live = this.data.live;
    live.start_time = e.detail.value
    this.setData({live});
  },
  bindEndTimeChange: function(e) {
    let live = this.data.live;
    live.end_time = e.detail.value
    this.setData({live});
  },
  getInput(event){
    let live = this.data.live;
    live.title = event.detail.value
    this.setData({live});
  },

  onChangeTap(event){
    let live = this.data.live;
    live.live_img = event.detail.current[0];
    this.setData({live})
  },

  submit(event){
    const {detail} = event;
    let _this = this;
    let live = this.data.live;
    live.is_record = detail.values.is_record;
    this.setData({live})
    console.log(live.live_img);
    wx.uploadFile({
      url: 'http://www.admin.com/api/upfile', //仅为示例，非真实的接口地址
      filePath: live.live_img,
      name: 'file',
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        'token': wx.getStorageSync('token').token
      },
      success: (res) => {
        live.live_img  = JSON.parse(res.data).data;
         http.request("live/recording/create",live,"post").then((res) => {
           console.log(res.data);
          _this.setData({show: true});
          setTimeout(function() {
            wx.redirectTo({
              url: '../edit/edit?id='+res.data,
            })
          },2000)
    })
      },
      fail(res) {
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.lin.initValidateForm(this)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})